<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="登录账户设置" name="first">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="form.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码" prop="pwd2">
                <el-input v-model="form.pwd2" type="password" style="width:300px" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserDetailById">更新</el-button>
                <el-button>取消</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <el-tab-pane label="个人详情" name="second">
            <!-- 动态组件 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="UserComponent" :user-id="$route.query.id" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息" name="third">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <jobInfo :user-id="$route.query.id" />
          </el-tab-pane>
        </el-tabs>
      </el-card>

    </div>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employees'
import userInfo from './components/user-info.vue'
import jobInfo from './components/job-info.vue'
export default {
  components: {
    userInfo,
    jobInfo
  },
  data() {
    return {
      UserComponent: 'userInfo',
      activeName: 'first',
      form: {
        username: '',
        pwd2: ''
      },
      rules: {
        username: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
        pwd2: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码格式应为6-9位', trigger: 'blur' }]
      },
      userId: this.$route.query.id
    }
  },
  created() {
    // console.log(this.$route.query)
    this.form = this.$route.query
  },
  methods: {
    async saveUserDetailById() {
      try {
        await this.$refs.form.validate()
        await saveUserDetailById({ ...this.form, password: this.form.pwd2 })
        this.$message.success('修改成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
